﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace lchealthy
{
    public partial class frminshop : Form
    {
        int pid = 1;//产品的ID号
        DataSet ds, dsBuy;
        int userid = 0;
        int flg;
        public frminshop(int id)
        {
            userid = id;
            InitializeComponent();
        }
        private void proBind()
        {
            string sql = "SELECT ProductInfo.PID, ProductInfo.PName, indepositinfo.dPnum FROM indepositinfo INNER JOIN ProductInfo ON indepositinfo.PID = ProductInfo.PID where indepositinfo.cid= " + userid;
            ds = DbHelperOleDb.Query(sql);
            dgvPro.AutoGenerateColumns = false;
            dgvPro.DataSource = ds.Tables[0];
            //单元格内内容比较多时，设置自动换行
            //dgvPro.DefaultCellStyle.WrapMode = DataGridViewTriState.True;
            //设置根据内容的多少自动调整单元格的高度
            dgvPro.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;
        }
        //单击产品列表表格时
        private void dgvPro_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            int row = -1;
            row = dgvPro.CurrentRow.Index;
            //得到选中产品的ID
            pid = int.Parse(ds.Tables[0].Rows[row]["PID"].ToString());
            string proName = ds.Tables[0].Rows[row]["PName"].ToString();
            lblProName.Text = proName;
            txtNum.Focus();           
        }
        //创建购物车的表   
        public void createTable()
        {
            dsBuy = new DataSet();
            using (DataTable dt = new DataTable("buy"))
            {    //创建列            
                DataColumn dtc = new DataColumn("产品ID", typeof(string));
                dt.Columns.Add(dtc);
                dtc = new DataColumn("pname", typeof(string));
                dt.Columns.Add(dtc);
                dtc = new DataColumn("pnum", typeof(int));
                dt.Columns.Add(dtc);
                dsBuy.Tables.Add(dt);
            }
            dgvBuy.AutoGenerateColumns = false;
            //dgvPro.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;
            dgvBuy.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
        }        
        //点击确定第二表（购物车）生成数据
        private void btnConfirm_Click(object sender, EventArgs e)
        {
            bool ishave = false;
            for (int i = 0; i < dsBuy.Tables[0].Rows.Count; i++)
            {
                if (pid.ToString() == dsBuy.Tables[0].Rows[i][0].ToString())
                    ishave = true;
            }
            if (!ishave)
            {
                string proName = ds.Tables[0].Rows[dgvPro.CurrentRow.Index]["PName"].ToString();                
                int num = int.Parse(txtNum.Text);               
                DataRow dr = dsBuy.Tables[0].NewRow();
                dr[0] = pid;
                dr[1] = proName;
                dr[2] = num;                
                dsBuy.Tables[0].Rows.InsertAt(dr, dsBuy.Tables[0].Rows.Count );
                dsBuy.Tables[0].AcceptChanges();
                dgvBuy.DataSource = dsBuy.Tables[0];
                dgvBuy.Columns[0].ReadOnly = true;
                dgvBuy.Columns[1].ReadOnly = true;
                dgvBuy.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
            }
            else
            {
                MessageBox.Show("已购消费过这个产品，可以直接修改数量！");
            }
        }
        //当客户选择的产品数量有变化时候，产品总价格也要变化
        private void dgvBuy_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            //getsum();
        }       
        private void dgvBuy_UserDeletingRow(object sender, DataGridViewRowCancelEventArgs e)
        {            

        }
       
        private void txtNum_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                btnConfirm_Click(null, null);
            }
        }
        //产品消费，积分
        private void btnBuy_Click(object sender, EventArgs e)
        {
            int bpid = 0, num = 0;
            string pname;
            string sql = "";           
            for (int i = 0; i < dsBuy.Tables[0].Rows.Count; i++)
            {
                bpid = int.Parse(dsBuy.Tables[0].Rows[i][0].ToString());
                num = int.Parse(dsBuy.Tables[0].Rows[i][2].ToString());
                pname = dsBuy.Tables[0].Rows[i][1].ToString();
                sql = string.Format("insert into useinfo(pid,cid,unum) values({0},{1},{2})", bpid, userid, num);
                DbHelperOleDb.ExecuteSql(sql);                 
            }
            if (ckbfen.Checked)
            {
                sql = string.Format("insert into jifeninfo(cid,jid) values({0},{1})", userid, ddlfen.SelectedValue);
                DbHelperOleDb.ExecuteSql(sql);
            }
            MessageBox.Show("增加成功！");           
            this.Close();
        }
        private void frminshop_Load(object sender, EventArgs e)
        {            
            proBind();
            createTable();
            bind();
        }
        private void bind()
        {
            string sql = "select id,jifentext+'('+cstr(jnum)+')' as fen from jifenconfig";
            DataSet ds = DbHelperOleDb.Query(sql);
            ddlfen.DataSource = ds.Tables[0];
            ddlfen.DisplayMember = "fen";
            ddlfen.ValueMember = "id";
        }
        private void dgvPro_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }
    }
}
